home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 420_01 / common.c < prev    next >
C/C++ Source or Header  |  1994-02-21  |  2KB  |  107 lines

  1. /*
  2. //    common.cc -- $@6&DL$J;q8;(J
  3. //
  4. //        created    in 2/14/1994
  5. */
  6.  
  7. #include    <ctype.h>
  8. #include    "defs.h"
  9.  
  10. int16    *Screen = NULL;
  11. int32    Width, Height, Size;
  12. FILE    *fp;
  13. jmp_buf    ErrorEntry;
  14. char    *Program, *File;
  15. unsigned char    *BitBuffer;
  16. uint32     BitCache;
  17. int     BitLength;
  18. int     BitBufferLength;
  19. unsigned char    BitField[8] = { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 };
  20. int     MaxColorGuaranteed;
  21. int16    Red[256 + 4], Green[256 + 4], Blue[256 + 4], Palette[256 + 4];
  22. char    *Extension[NumberofFormats];
  23. char    *Format[NumberofFormats];
  24.  
  25. void    Initialize()
  26. {
  27.     Extension[UNKNOWN] = NULL;
  28.     Extension[ALPHA] = "alp";
  29.     Extension[BETA] = "bet";
  30.     Extension[GAMMA] = "gam";
  31.     Extension[DELTA] = "dlt";
  32.     Extension[PIC] = "pic";
  33.     Extension[MAG] = "mag";
  34.     Extension[MAKI] = "mki";
  35.     Extension[PI] = "pi";
  36.     Extension[ML1] = "ml1";
  37.     Extension[PPM] = "ppm";
  38.     Extension[PBM] = "pbm";
  39.     Format[UNKNOWN] = NULL;
  40.     Format[ALPHA] = "alpha";
  41.     Format[BETA] = "beta";
  42.     Format[GAMMA] = "gamma";
  43.     Format[DELTA] = "delta";
  44.     Format[PIC] = "pic";
  45.     Format[MAG] = "mag";
  46.     Format[MAKI] = "maki";
  47.     Format[PI] = "pi";
  48.     Format[ML1] = "ml1";
  49.     Format[PPM] = "ppm";
  50.     Format[PBM] = "pbm";
  51. }
  52.  
  53. void    BitBufferLoad()
  54. {
  55.     fread( BitBuffer, 1, BitBufferSize, fp );
  56.     BitBufferLength = 0;
  57. }
  58.  
  59. void    BitBufferFlush()
  60. {
  61.     fwrite( BitBuffer, 1, BitBufferLength, fp );
  62.     BitBufferLength = 0;
  63. }
  64.  
  65. void    BitBufferLastFlush()
  66. {
  67.     if ( BitLength != 0 ) {
  68.     BitCache <<= 32 - BitLength;
  69.     BitBuffer[BitBufferLength++] = BitCache & 0xff;
  70.     BitBuffer[BitBufferLength++] = (BitCache >> 8) & 0xff;
  71.     BitBuffer[BitBufferLength++] = (BitCache >> 16) & 0xff;
  72.     BitBuffer[BitBufferLength++] = (BitCache >> 24) & 0xff;
  73.     }
  74.     fwrite( BitBuffer, 1, BitBufferLength, fp );
  75. }
  76.  
  77. int     istrcmp( char *s1, char *s2 )
  78. {
  79.     int     c1, c2;
  80.  
  81.     if ( *s1 == '\0' && *s2 == '\0' )
  82.     return 0;
  83.     if ( *s1 == '\0' || *s2 == '\0' )
  84.     return 1;
  85.     c1 = isupper (*s1)? tolower (*s1) : *s1;
  86.     c2 = isupper (*s2)? tolower (*s2) : *s2;
  87.     if ( c1 != c2 )
  88.     return 1;
  89.     return istrcmp( s1 + 1, s2 + 1 );
  90. }
  91.  
  92. int     InferFormat( char *File )
  93. {
  94.     int     Length = strlen (File);
  95.     int     i;
  96.  
  97.     if ( Length < 4 )
  98.     return UNKNOWN;
  99.     for ( i = 1; i < NumberofFormats; i++ ) {
  100.     if ( i == PI && istrcmp( Extension[i], File + Length - 2 ) == 0 )
  101.         return PI;
  102.     if ( i != PI && istrcmp( Extension[i], File + Length - 3 ) == 0 )
  103.         return i;
  104.     }
  105.     return UNKNOWN;
  106. }
  107.